<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Shuadanshou extends AdminController{
    public $_validation = null;
    function __construct(){
        parent::__construct();
        $this->load->model('MShuadanshou');

        $this->_validation = Array(
            array('field'=>'type', 'label'=>'刷手类型', 'rules'=>'required|integer')
            ,array('field'=>'name', 'label'=>'刷手名称', 'rules'=>'trim|required|max_length[255]')
            ,array('field'=>'account', 'label'=>'刷手收款账号', 'rules'=>'trim|required|max_length[255]')
            ,array('field'=>'account_desc', 'label'=>'收款账号详情', 'rules'=>'trim|required|max_length[255]')
            ,array('field'=>'reviews', 'label'=>'刷手评分', 'rules'=>'trim|max_length[4]')
            ,array('field'=>'remark', 'label'=>'备注', 'rules'=>'max_length[255]')
            ,array('field'=>'status', 'label'=>'状态', 'rules'=>'integer')
            ,array('field'=>'update_time', 'label'=>'更新时间', 'rules'=>'')
        );
    }

    function index(){

        $this->load->library('FormFilter');
        $this->load->helper('formfilter');
        $this->formfilter->addFilter('s.type','where');
        $this->formfilter->addFilter('s.status','where');
        $this->formfilter->addFilter('s.name','like');
        $this->formfilter->addFilter('s.account','like');

        if($begintime = filterValue('shuandanshou_create_time_begin')){
            $this->formfilter->addFilter('shuandanshou_create_time_begin', 'where',array('s.create_time >= ',strtotime($begintime.' 00:00:00')));
        }
        if($endtime = filterValue('shuandanshou_create_time_end')){
            $this->formfilter->addFilter('shuandanshou_create_time_end', 'where',array('s.create_time <= ',strtotime($endtime.' 23:59:59')));
        }

        $limit = $this->pagination($this->MShuadanshou->getCount());
        $list = $this->MShuadanshou->getList($limit);

        $this->assign('list', $list);
        $this->layout();
    }

    function add(){
        if(!$this->p->add){
            //权限不足
            show_error(l('user_has_nopower'));
        }
        $obj = $this->MShuadanshou->createVo();
        $this->load->library('form_validation');
        $this->form_validation->set_rules($this->_validation);
        if($this->form_validation->run()==TRUE){
            $this->load->helper('populate');
            $newObj = populate((Object)((Array)$obj), $this->form_validation->post());
            $newObj->admin_id = $this->_user->id;
            if($this->MShuadanshou->add($newObj)){
                successAndRedirect(l('add_success'));
            }else{
                errorAndRedirect(l('data_fail'));
            }
        }
        $this->assign('obj', $obj);
        $this->layout();
    }

    function delete($id, $update_time){
        if(!is_numeric($id) || !is_numeric($update_time)){
            show_error(l('id_not_null'));
        }
        if(!$this->MShuadanshou->delete($id, $update_time)){
            errorAndRedirect(l('data_fail'));
        }
        successAndRedirect(l('delete_success'));
    }

    function edit($id){
        if(!$this->p->edit){
            //权限不足
            show_error(l('user_has_nopower'));
        }
        if(((int)$id) . '' !== $id){
            show_error(l('id_or_updated_not_null'));
        }
        $obj = $this->MShuadanshou->getOne(Array('id'=>$id));
        if(!$obj){
            show_error('此刷手不存在！');
        }
        $this->load->library('form_validation');
        $this->form_validation->set_rules($this->_validation);
        if($this->form_validation->run()==TRUE){
            $this->load->helper('populate');
            $newObj = populate($obj, $this->form_validation->post());
            if($this->MShuadanshou->update($newObj)){
                successAndRedirect(l('edit_success'));
            }
            //操作冲突
            error(l('data_fail'));
        }

        $this->assign('obj', $obj);
        $this->layout();
    }
}